// Server.h: interface for the Server class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SERVER_H__80A53B65_6421_4504_BE62_77247AE577A8__INCLUDED_)
#define AFX_SERVER_H__80A53B65_6421_4504_BE62_77247AE577A8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "CommonLib\Packet.h"

#include "MonitoringUnitLib\CoalMineUnitManager.h"
#include "..\..\CommonLibs\CommonLib\Log.h"
//2005-8-10
#include "ADO.h"

class CLog;
class Markup;
class CoalMineUnit;
class CDatabase;
struct MonUnitDef;
struct MonUnitDefEx;
struct MonUnitDefNormal;
struct ExcessRecord;
struct DefinitionUpdateStructNormal;

class Server : public CoalMineUnitManager
{
protected:
	Server();
	virtual ~Server();

public:
	void SaveAllRecordsToExcessDB() const;
	static Server * Instance();
	static void DeleteInstance();

	void Init();
	void Cleanup();
	CMutex m_mutexSaveData;//2005-6-4
	int m_nMaxNumOfCoalMine;
	bool bKeyTestOk;

	// Implementations
	bool UpdateDatabase(CAdoConnection * m_pAdoConnection, bool bSave = true);
	void BackupDefinitionToFile() const;
	void BackupDefinitionToFileEx() const;

	void MakeRecordIndex() const;
	void MakeRecordIndexEx() const;

	void MigrationRecordIndexEx() const;

	void FlushRecords()  const;
	void SaveAllRecords(bool bHalt = false) const;
	void HandleTimer() const ;
	bool GetDefinitionFromFile(const CTime& time, long nCoalMineId, const CString& strMonUnitId, MonUnitDef * pMonUnitDef) const;
	bool GetDefinitionFromFile(const CTime& time, long nCoalMineId, const CString& strMonUnitId, MonUnitDefEx * pMonUnitDef) const;

	DWORD GetRecordIndex(int nYear, int nMonth, int nDay, long nCoalMineId) const;
	DWORD GetRecordIndexEx(int nYear, int nMonth, int nDay, long nCoalMineId) const;

	void AutoGenerateReport() const;
	void AutoReportConfig();
	void HandleRequest(const Packet& packetReq, HANDLE hClient);

	//2005-8-12
	bool SaveConnectionStatusToDB(BaseCoalMineUnit * pCoalMineUnit, bool bDelete = false)  ;
	void SaveAlarmInfoToDB(DefinitionUpdateStructNormal * pDefinitionUpdateStructNormal);
	//CAdoConnection m_adoConnection;
	CAdoRecordSet  m_adoRecordSet;
	CAdoConnection *m_adopConnection;

	void RainbowInit() ;
	void RainbowCheck() const;
	void RainbowCount() ;

	bool m_bAdoConnect;

	bool m_bStanderdUpperLimit;

	CLog *pLog;

	void InitDataDir();

private:
	static Server * m_pInstance;
};

#endif // !defined(AFX_SERVER_H__80A53B65_6421_4504_BE62_77247AE577A8__INCLUDED_)
